package com.yanqu.road.server.cmd.vehicle;

import com.yanqu.road.logic.pb.VehiclePb;
import com.yanqu.road.entity.config.vehicle.VehicleModifySkillResult;
import com.yanqu.road.pb.vehicle.VehicleProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_VEHICLE_SKILL_MODIFY_AGREE, desc = "座骑技能改装，替换技能")
public class VehicleSkillModifyAgreeCmd extends AbstractCommand {

	private String vehicleUid;

	private int skillPos;

	private boolean isAgree;

	private VehicleModifySkillResult result;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		VehicleProto.VehicleSkillModifyAgreeReqMsg reqMsg = VehicleProto.VehicleSkillModifyAgreeReqMsg.parseFrom(packet.getBytes());
		vehicleUid = reqMsg.getVehicleUid();
		skillPos = reqMsg.getSkillPos();
		isAgree = reqMsg.getIsAgree();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(isAgree) {
			result = player.getModule(VehicleModule.class).agreeModifySkill(vehicleUid, skillPos);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		VehicleProto.VehicleSkillModifyAgreeRespMsg.Builder respMsg = VehicleProto.VehicleSkillModifyAgreeRespMsg.newBuilder();
		if(null != result){
			respMsg.setRet(result.getRet());
			if(0 == result.getRet()){
				respMsg.setVehicleUid(vehicleUid);
				VehicleProto.VehicleSkillTempMsg.Builder skillMsg = VehiclePb.parseVehicleSkillTempMsg(result.getVehicleSkill());
				if(null != skillMsg) {
					respMsg.setSkill(skillMsg);
				}
			}
			player.sendPacket(Protocol.U_VEHICLE_SKILL_MODIFY_AGREE, respMsg);
		}
	}
}
